package com.it.demo.mq.producer;

import org.apache.activemq.ScheduledMessage;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessagePostProcessor;
import org.springframework.stereotype.Service;

import javax.jms.JMSException;
import javax.jms.Message;

/**
 * @author fushihao
 *
 * 延时队列
 */
@Service
public class ExpireProducer {

    @Autowired
    private JmsTemplate jmsTemplate;

    public void dalyTask() {
        ActiveMQQueue activeMQQueue = new ActiveMQQueue("order.expire.queue");
        jmsTemplate.convertAndSend(activeMQQueue, "{\"id\":1}", new MessagePostProcessor() {

            @Override
            public Message postProcessMessage(Message message) throws JMSException {
                message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, 20 * 1000);
                return message;
            }
        });
    }
    }
